#!/bin/bash

POSTGIS_SRC=../../..
RTGDAL=${POSTGIS_SRC}/raster/scripts/python/rtgdalraster.py
DB=postgis_reg
DBENABLERS="${POSTGIS_SRC}/postgis/postgis.sql ../../rt_pg/rtpostgis.sql"

PSQL=`which psql`
GDALINFO=`which gdalinfo`

OPT_DROP=yes
OPT_CREATE=yes

# create temp
if [ -z "$TMPDIR" ]; then
	TMPDIR=/tmp/pgis_reg_$$
fi

mkdir -p ${TMPDIR}
echo "TMPDIR is ${TMPDIR}"
echo

TESTFILE=${POSTGIS_SRC}/raster/test/regress/testgdalraster.test
EXPECTEDFILE=${POSTGIS_SRC}/raster/test/regress/testgdalraster_expected
CHECKSUMFILE=${TMPDIR}/checksum_log
DIFFFILE=${TMPDIR}/checksum_diff

# testfile doesn't exist
if [ ! -f $TESTFILE ]; then
	exit 1
fi

while [ -n "$1" ]; do
	if test "$1" = "--nodrop"; then
		OPT_DROP=no
		shift
		continue
	elif test "$1" = "--nocreate"; then
		OPT_CREATE=no
		shift
		continue
	else
		break
	fi
done

# create database
db_exists=`${PSQL} -l | grep -w ${DB}`
if test -z "$db_exists"; then
       	if test x"$OPT_CREATE" = "xyes"; then
		echo "Creating spatial db ${DB} "
		
		createdb "${DB}" > ${TMPDIR}/regress_log
		createlang plpgsql "${DB}" >> ${TMPDIR}/regress_log
		for f in $DBENABLERS; do
			${PSQL} -f $f "${DB}" >> ${TMPDIR}/regress_log 2>&1
		done
	else
		
		echo "Database ${DB} does not exist" >&2
		echo "Run w/out the --nocreate flag to create it" >&2
		exit 1
	fi
else
	if test x"$OPT_CREATE" = "xyes"; then
		echo "Database ${DB} already exist." >&2
		echo "Run with the --nocreate flag to use it " \
			"or drop it and try again." >&2
		exit 1
	else
		echo "Using existing database ${DB}"
	fi
fi

libver=`${PSQL} -tAc "select postgis_lib_version()" "${DB}"`
if [ -z "$libver" ]; then
	echo
	echo " Something went wrong (no postgis installed in ${DB})."
	if [ -z "$db_exists" ]; then
		echo " For details, check ${TMPDIR}/regress_log"
	else
		echo " Try dropping the database, it will be recreated" \
			" on next run."
	fi
	echo
	exit 1
fi

rtlibver=`${PSQL} -tAc "select postgis_raster_lib_version()" "${DB}"`
if [ -z "$rtlibver" ]; then
	echo
	echo " Something went wrong (no raster installed in ${DB})."
	if [ -z "$db_exists" ]; then
		echo " For details, check ${TMPDIR}/regress_log"
	else
		echo " Try dropping the database, it will be recreated" \
			" on next run."
	fi
	echo
	exit 1
fi

LC=`wc -l $TESTFILE | awk '{print $1}'`
for x in `seq $LC`; do
	ln=`head -n $x $TESTFILE | tail -n 1`

	FORMAT=GTiff
	OPTS=
	RASTER=

	while IFS=';' read -ra ELE; do
		j=0
		for i in "${ELE[@]}"; do
			if [ ${#i} -ne 0 ]; then
				case "$j" in
					"0")
						FORMAT="$i"
					;;
					"1")
						OPTS="$i"
					;;
					"2")
						RASTER="$i"
					;;
				esac
			fi

			let j=j+1
		done
	done <<< "$ln"

	if [ ${#RASTER} -eq 0 ]; then
		continue
	fi
	if [ ${#FORMAT} -ne 0 ]; then
		FORMAT="-f ${FORMAT}"
	fi
	if [ ${#OPTS} -ne 0 ]; then
		OPTS="-c ${OPTS}"
	fi

	#echo "format: "$FORMAT
	#echo "opts: "$OPTS
	#echo "raster: "$RASTER

	$RTGDAL -d "dbname=${DB}" -r "${RASTER}" ${FORMAT} ${OPTS} -o ${TMPDIR}/q${x} >> ${TMPDIR}/rtgdalraster_log

	$GDALINFO -checksum ${TMPDIR}/q${x} | grep Checksum | cut -c 3- >> ${CHECKSUMFILE}
done

if test x"$OPT_DROP" = "xyes" -a x"$OPT_CREATE" = "xyes"; then
	sleep 1
	dropdb "${DB}" > /dev/null
fi

echo
if diff -c "${EXPECTEDFILE}" "${CHECKSUMFILE}" > ${DIFFFILE}; then
	echo "Test passed."
else
	cat ${DIFFFILE}

	echo
	echo "Test failed."
fi
